import fs from 'fs';
import http from 'http';

let port=8080;

let a=http.createServer((req,res)=>{
    let tmpath=req.url;
    res.writeHead(200,{'content-type':'text/html;charset=utf-8'});
    let data=fn(tmpath);
    res.end(data);
});
a.listen(port,()=>{
    console.log(`监听成功，服务器运行在http://localhost:${port}`);
})

function fn(filepath){
    let res='很遗憾';
    if(filepath !=='/favicon.ico'){
        let apath='.'+filepath;
        let a='.'+filepath+'/'+'index.html';
        let c=fs.existsSync(a);
        if(c){
            res=fs.readFileSync(a,'utf-8');
        }else{
            let d=fs.readdirSync(apath);
            d.forEach((item)=>{
                let spath='.'+filepath+'/'+item;
                let f=fs.statSync(spath);
                if(f.isDirectory()){
                    res=fn(spath);
                }
            })
        }
    }
}